package com.cheng.cms.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * 文件上传工具类
 */
public class FileUploadUtil {
    /**
     * 图片上传
     * @param file  图片文件前端传过来
     * @param file_path  文件路径
     * @return
     */
    public static String pictureUpload(MultipartFile file,String file_path){
        String result = "200";
        if(file.isEmpty()){
            result = "文件为空";
        }
        //文件名
        String fileName = file.getOriginalFilename();
        //获取后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //上传路径
        String filePath = file_path;
        //新文件名
        fileName = UUID.randomUUID()+suffixName;
        File dest = new File(filePath+fileName);
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdir();
        }
        try{
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
        }
        //String path = filePath+fileName;
        //System.out.println(result);
        //System.out.println(path);
        //System.out.println("收到上传头像请求");
        return result;
    }
}
